#include("../../layout/header.html")
#@layout()
#define content()
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header"><h2>站内信管理</h2></div>
        <div class="layui-card-body">
            <!-- 表格搜索栏 -->
            <form id="form" class="layui-form toolbar">
                <div class="layui-form-item">

                    <div class="layui-inline">
                        <label class="layui-form-label">类型:</label>
                        <div class="layui-input-inline">
                            <select id="typeSelect" name="type" lay-search>
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="dataTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="refresh" class="layui-btn icon-btn layui-btn-warm">
                            <i class="layui-icon layui-icon-refresh"></i>
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>
<!-- 表单弹窗 -->
<script type="text/html" id="dataEditDialog">
    <form id="dataEditForm" lay-filter="dataEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">类型:</label>
                    <div class="layui-input-block">
                        <select id="type" name="type" lay-filter="type" lay-search>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">标题:</label>
                    <div class="layui-input-block">
                        <input name="title" class="layui-input" placeholder="请输入标题 默认类型"/>
                    </div>
                </div>


                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label layui-form-required">内容:</label>
                    <div class="layui-input-block">
                        <textarea name="message" placeholder="请输入内容" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">展示时间:</label>
                    <div class="layui-input-block">
                        <input name="timeout" type="number" class="layui-input"
                               placeholder="请输入展示时间 默认不关闭"/>
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">展示位置:</label>
                    <div class="layui-input-block">
                        <select id="position" name="position" lay-search>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">展示图片:</label>
                    <div class="layui-input-block">
                        <input name="image" class="layui-input" placeholder="请输入展示图片"/>
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">跳转链接:</label>
                    <div class="layui-input-block">
                        <input name="linkUrl" class="layui-input" placeholder="请输入跳转链接地址"/>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">

                <div class="layui-form-item">
                    <label class="layui-form-label">发送对象:</label>
                    <div id="sendRadio" class="layui-input-block">

                    </div>
                </div>

                <div id="objectValueDiv1" class="layui-form-item layui-hide">
                    <label class="layui-form-label">对象值:</label>
                    <div class="layui-input-block">
                        <div id="objectValueDiv2" class="layui-hide"></div>
                        <div id="objectValueDiv3" class="layui-hide"></div>
                    </div>
                </div>

            </div>
        </div>




        <div class="layui-form-item text-right ai-form-footer">
            <button class="layui-btn" lay-filter="dataEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="dataTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit" perm-show="message:update">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="record" perm-show="message:record">发送记录</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" perm-show="message:del">删除</a>
</script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'formX', 'laydate', 'tableX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var tableX = layui.tableX;
        var formX = layui.formX;
        var laydate = layui.laydate;
        var admin = layui.admin;
        var messageType = [], messagePosition = [] , messageSendType = [],orgs = [],users = [];

        admin.getDict({type: 'msgType,msgPosition,msgSendType'}, function (d) {
            if (0 === d.code) {

                messageType = d.data.msgType;
                formX.renderSelect({
                    elem: '#typeSelect',
                    data: messageType,
                    name: 'name',
                    value: 'value',
                    hint: '请选择类型'
                });
                messagePosition = d.data.msgPosition;
                messageSendType = d.data.msgSendType;


                /*最后渲染组件*/
                form.render();
            }
        })

        admin.req('/sys/sysOrg/selectData',{},function (d){if(0 === d.code){orgs = d.data;}})
        admin.req('/sys/sysUser/selectData',{},function (d){if(0 === d.code){users = d.data;}})


        laydate.render({
            elem: 'input[name="createTime"]',
            type: 'date',
            range: false,
            trigger: 'click'
        });
        laydate.render({
            elem: 'input[name="updateTime"]',
            type: 'date',
            range: false,
            trigger: 'click'
        });

        /* 渲染表格 */
        var insTb = tableX.init({
            url: '/sys/message/list',
            toolbar: ['<p>',
                admin.hasPerm('message:add') ? '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;' : '',
                admin.hasPerm('message:export') ? '<button lay-event="export" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon layui-icon-export"></i>导出</button>&nbsp;' : '',
                admin.hasPerm('message:del') ? '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>' : '',
                '</p>'].join(''),
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {type: 'numbers', title: '序号', fixed: 'left', minWidth: 80},
                {field: 'typeStr', title: '类型'},
                {
                    field: 'image', title: '展示图片', templet: function (d) {
                        if (!d.image) {
                            return '<i class="layui-icon layui-icon-speaker message-item-icon"></i>';
                        }
                    }
                },
                {field: 'title', title: '标题'},
                {field: 'message', title: '内容',minWidth: 300},
                {field: 'timeout', title: '展示时间'},
                {field: 'position', title: '展示位置'},
                {field: 'linkUrl', title: '跳转链接'},
                {field: 'createBy', title: '创建人'},
                {field: 'createTime', title: '创建时间'},
                {title: '操作', toolbar: '#dataTbBar', align: 'center', fixed: 'right', minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(dataTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }else if(obj.event === 'record'){
                admin.popupRight({
                    type: 2,
                    area: '45%',
                    content: '/view/module/sys/message-record',
                    data:{
                        messageId:obj.data.id
                    },
                    success: function (layero, dIndex) {
                    },
                    end: function () {
                    }
                });
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.popupRight({
                type: 1,
                area: '56%',
                title: (mData ? '修改 ' : '添加 ') + ((mData && mData.name) ? mData.name : "站内信"),
                content: $('#dataEditDialog').html(),
                success: function (layero, dIndex) {

                    formX.renderSelect({
                        elem: '#type',
                        data: messageType,
                        name: 'name',
                        value: 'value',
                        hint: '请选择类型'
                    });
                    formX.renderRadio({
                        elem: '#sendRadio',
                        data: messageSendType,
                        name: 'sendType',
                        title: 'name',
                        value: 'value',
                        filter: 'sendType'
                    })

                    form.on('select(type)', function (data) {
                        // console.log(data.elem[data.value].innerText);
                        $('input[name=title]').val(data.elem[data.value].innerText);
                    });

                    form.on('radio(sendType)', function(data){
                        console.log(data.elem); //得到radio原始DOM对象
                        console.log(data.value); //被点击的radio的value值
                        setObjectValue(data.value);
                    });

                    formX.renderSelect({
                        elem: '#position',
                        data: messagePosition,
                        name: 'name',
                        value: 'value',
                        initValue: 'topRight',
                        hint: '请选择类型'
                    });

                    if(mData){
                        setObjectValue(mData.sendType)
                    }
                    // 回显表单数据
                    form.val('dataEditForm', mData);


                    // 表单提交事件
                    form.on('submit(dataEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);


                        admin.post('/sys/message/add', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        });
                        return false;
                    });
                },
                end: function () {

                }
            });
        }

        function setObjectValue(value){
            if(value == 2){
                $('#objectValueDiv1').removeClass('layui-hide');
                $('#objectValueDiv2').removeClass('layui-hide');
                $('#objectValueDiv3').addClass('layui-hide');
                let insOrg = formX.renderMultiTreeSelect('#objectValueDiv2','objectValue',orgs,true);
            }else if(value == 3){
                $('#objectValueDiv1').removeClass('layui-hide');
                $('#objectValueDiv3').removeClass('layui-hide');
                $('#objectValueDiv2').addClass('layui-hide');
                let insUser = formX.renderMultiSelect({el:'#objectValueDiv3',name:'objectValue',value:'id',data:users})
            }else if(value == 1){
                $('#objectValueDiv1').addClass('layui-hide');
            }
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req('/sys/message/del', {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            });
        }

        $('#refresh').click(function () {
            window.location.reload()
        })

    });
</script>
</body>
#end
